package com.mediaideas.videoengine.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.util.Log;

import com.mediaideas.videoengine.AudioMixerParams;
import com.mediaideas.videoengine.AudioMixerService;

import java.io.File;

/**
 * Created by ming on 2015/10/31.
 */
public class VideoEngineUtils {
    private final static String TAG = "VideoEngineUtils";

    public static boolean loadVideoLibraries() {
        boolean result = false;
        try {
            System.loadLibrary("avutil-54");
            System.loadLibrary("swresample-1");
            System.loadLibrary("avcodec-56");
            System.loadLibrary("avformat-56");
            System.loadLibrary("swscale-3");
            System.loadLibrary("postproc-53");
            System.loadLibrary("avfilter-5");
            System.loadLibrary("VideoEngine");

//            ResponseTimeUtils.getInstance().stopTheClock();
//            Log.e("", "loadVideoLibraries Time:"
//                    + ResponseTimeUtils.getInstance().getResponseTime());
            result = true;
//            GAUtils.logAlertEvent(InstashotApplication.getAppContext(), "VideoUtils", "LoadLibraries", "End");
        } catch (Throwable e) {
//            Log.e("VideoUtils", AppUtils.getStackTrace(e));
//            GAUtils.logAlertEvent(InstashotApplication.getAppContext(), "VideoUtils", "LoadLibraries", "Failed");
            e.printStackTrace();
//            LogUtils.sendErrorLog(InstashotApplication.getAppContext(), e, false, null, false);
            result = false;
        }

        return result;
    }

    public static boolean isFileExists(String path) {
        if (path == null)
            return false;

        File f = new File(path);
        return f.exists();
    }

    public static boolean isFileExtensionNameValid(String fileName) {
        return fileName.endsWith(AudioMixerParams.AUDIO_SUFFIX_AAC);
    }

    /**
     * 绑定 混音服务
     *
     * @param activity
     * @param callback
     * @return
     */
    public static boolean bindToAudioMixerService(Activity activity, ServiceConnection callback) {
        Activity realActivity = activity.getParent();
        if (realActivity == null) {
            realActivity = activity;
        }
        ContextWrapper cw = new ContextWrapper(realActivity);
        cw.startService(new Intent(cw, AudioMixerService.class));
        Intent intent = new Intent();
        intent.setClass(cw, AudioMixerService.class);
        if (cw.bindService(intent, callback, 0)) {
            Log.e(TAG, "sucess to bind to audio mixer service");
            return true;
        }
        Log.e(TAG, "Failed to bind to audio mixer service");
        return false;
    }

    /**
     * 解绑 混音服务
     *
     * @param context
     * @param callback
     */
    public static void unbindFromeAudioMixerService(Activity context, ServiceConnection callback) {
        if (callback != null) {
            context.unbindService(callback);
            Log.e(TAG, "unbind audio mixer service");
        }
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
    public static long getMediaDuration(String mediaFilePath) {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        String duration = "";
        try {
            mmr.setDataSource(mediaFilePath);
            duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            return Long.valueOf(duration);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } finally {
            try {
                mmr.release();
                mmr = null;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

}
